package org.yu.register.item;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import org.jetbrains.annotations.Nullable;
import org.yu.register.CapabilityRegister;
import org.yu.register.capability.ItemMixedCapability;
import org.yu.register.capability.ItemMixedProvider;

import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class DanYao extends Item {
    public DanYao(Properties pProperties) {
        super(pProperties.stacksTo(1)
                .food(new FoodProperties.Builder()
                        .alwaysEat()
                        .nutrition(0)
                        .saturationMod(0)
                        .build())
        );
    }

    @Override
    public ItemStack finishUsingItem(ItemStack pStack, Level pLevel, LivingEntity pLivingEntity) {
        pStack.getCapability(CapabilityRegister.MIXED_ITEM)
                .ifPresent(mixedCapability -> mixedCapability.applyEffect(pLivingEntity));
        return super.finishUsingItem(pStack, pLevel, pLivingEntity);
    }

    @Nullable
    @Override
    public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) {
        return new ItemMixedProvider(ItemMixedCapability::new);
    }
    @Nullable
    @Override
    public CompoundTag getShareTag(ItemStack stack) {
        super.getShareTag(stack);
        var tag = stack.getOrCreateTag();
        stack.getCapability(CapabilityRegister.MIXED_ITEM).ifPresent(mixedCapability -> {
            tag.put("Mix",mixedCapability.serializeNBT());
        });
        return tag;
    }

    @Override
    public void readShareTag(ItemStack stack, @Nullable CompoundTag nbt) {
        super.readShareTag(stack, nbt);
        if (nbt == null) {
            return;
        }
        stack.getCapability(CapabilityRegister.MIXED_ITEM).ifPresent(mixedCapability -> {
            mixedCapability.deserializeNBT(nbt.getCompound("Mix"));
        });
    }
}
